// 用户管理
var UserAddress = Ext.data.Record.create([
	{name : 'id',mapping : 'id',type : 'int'},  
	{name : 'orgName',mapping : 'orgName',type : 'string'}, 
	{name : 'name',mapping : 'name',type : 'string'}, 
	{name : 'email',mapping : 'email',type : 'string'}, 
	{name : 'phone',mapping : 'phone',type : 'string'}, 
	{name : 'mobile',mapping : 'mobile',type : 'string'}, 
	{name : 'address',mapping : 'address',type : 'string'}, 
	{name : 'zipCode',mapping : 'zipCode',type : 'string'}, 
	{name : 'sex',mapping : 'sex',type : 'string'}, 
	{name : 'birthday',mapping : 'birthday',type : 'date', dateFormat:'Y-m-d'}, 
	{name : 'duty',mapping : 'duty',type : 'string'}, 
	{name : 'standing',mapping : 'standing',type : 'string'},
	{name : 'status',mapping : 'status',type : 'string'},
	{name : 'age',mapping : 'age',type : 'int'}]);
// status , standing
var cm_user_address =  new Ext.grid.ColumnModel([
	new Ext.grid.CheckboxSelectionModel(),
	new Ext.grid.RowNumberer({header : '序号', width : 40}), 
    {id:'name',header: "姓名", width: 80, sortable: true, dataIndex: 'name'},
    {id:'orgName',header: "部门", width: 160, sortable: true , dataIndex: 'orgName', 
    	renderer: function(value, p, r) {
			var ret = '';
			var size = ds_org_select.getCount();
			for (var i = 0; i < size; i++) {
				var record = ds_org_select.getAt(i);
				var id = record.get('id');
				if (id == value) {
					ret = record.get('name');
					break;
				}
			}
			return (ret == '' || ret == null) ? value : ret;
		}},
    {id:'email',header: "email", width: 100,dataIndex: 'email'},
    {id:'phone',header: "电话", width: 80,dataIndex: 'phone'},
    {id:'mobile',header: "手机", width: 80,dataIndex: 'mobile'},
    {id:'zipCode',header: "邮编", width: 80,dataIndex: 'zipCode'},
    {id:'sex',header: "性别", width: 60,dataIndex: 'sex'},
//    {id:'birthday',header: "出生年月", width: 80,dataIndex: 'birthday', renderer: Ext.util.Format.dateRenderer('Y-m-d')},
//    {id:'age',header: "年龄", width: 20, dataIndex: 'age'},
    {id:'duty',header: "职务", width: 80,dataIndex: 'duty'},
    {id:'standing',header: "人员身份", width: 100,dataIndex: 'standing'},
    {id:'status',header: "状态", width: 60,dataIndex: 'status',
    	renderer : function(v) {
				var ret = '';
				switch (v) {
					case 'publish_no' :
						ret = '未发布';
						break
					case 'publish_yes' :
						ret = '已发布';
						break
					default :
				}
				return ret;
			}
    },
    {id:'address',header: "家庭住址", width: 200,dataIndex: 'address'}
]);

var btn_add_user_address = new Ext.Button({
	text : '添加',
	iconCls : 'icon-add',
	handler : function() {
		window_add_user_address.show();
		window_add_user_address.center();
	}
});

var btn_edit_user_address = new Ext.Button({
	text : '修改',
	iconCls : 'icon-edit',
	handler : function() {
		if (!grid_user_address.checkOne()) {
			return;
		}
		window_edit_user_address.show();
		window_edit_user_address.center();
		var record = grid_user_address.getSelectionModel().getSelected();
		// Ext.getCmp("form_edit_org").getForm().loadRecord(record);
		var frm = Ext.getCmp("form_edit_user_address").getForm();
		// set value 
		frm.findField('userAddress.id').setValue(record.get("id"));
		frm.findField('userAddress.orgName').setValue(record.get("orgName"));
		frm.findField('userAddress.name').setValue(record.get("name"));
		frm.findField('userAddress.email').setValue(record.get("email"));
		frm.findField('userAddress.phone').setValue(record.get("phone"));
		frm.findField('userAddress.mobile').setValue(record.get("mobile"));
		frm.findField('userAddress.address').setValue(record.get("address"));
		frm.findField('userAddress.zipCode').setValue(record.get("zipCode"));
		frm.findField('userAddress.sex').setValue(record.get("sex"));
//		frm.findField('userAddress.birthday').setValue(record.get("birthday"));
		frm.findField('userAddress.duty').setValue(record.get("duty"));
//		frm.findField('userAddress.age').setValue(record.get("age"));
		frm.findField('userAddress.standing').setValue(record.get("standing"));
		frm.findField('userAddress.status').setValue(record.get("status"));
	}
});

var btn_del_user_address = new Ext.Button({
	text : '删除',
	iconCls : 'icon-del',
	handler : function() {
		var record = grid_user_address.getSelectionModel().getSelected();
		if (record) {
			Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
						url : '/supervise/biz/useraddress/deleteUserAddress.action',
						params : {uerAddressId : record.data.id},
						success : function() {
							grid_user_address.getStore().remove(record);
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '删除时发生错误!',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			});
		}
	}
});
var btn_publish_user_address = new Ext.Button({
	text : '发布',
	iconCls : 'icon-add',
	handler : function() {
		if(!grid_user_address.checkMany()){
			return;
		}
		var records = grid_user_address.getSelectionModel().getSelections();
		var ids = [];
		for(var i = 0, count = records.length; i < count; i++){
			ids.push(records[i].get('id'));
		}
	//		提交保存数据
		Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
		Ext.Ajax.request({
			url : '/supervise/biz/useraddress/publish.action',
			success : function(response, options) {
				Ext.getBody().unmask();
				Ext.Msg.alert('提示', '成功发布数据！');
			 
			},
			failure : function() {
				Ext.getBody().unmask();
				window_edit_goalcategory.hide();
				Ext.Msg.alert('提示', '数据保存失败！');
			},
			params : {
				ids : ids.join(',')
			}
		});
	}
});
var btn_recall_user_address = new Ext.Button({
	text : '撤消',
	iconCls : 'icon-del',
	handler : function() {
		if(!grid_user_address.checkMany()){
			return;
		}
		var records = grid_user_address.getSelectionModel().getSelections();
		var ids = [];
		for(var i = 0, count = records.length; i < count; i++){
			ids.push(records[i].get('id'));
		}
	//		提交保存数据
		Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
		Ext.Ajax.request({
			url : '/supervise/biz/useraddress/recall.action',
			success : function(response, options) {
				Ext.getBody().unmask();
			 	Ext.Msg.alert('提示', '成功撤消发布！');
			},
			failure : function() {
				Ext.getBody().unmask();
				window_edit_goalcategory.hide();
				Ext.Msg.alert('提示', '数据保存失败！');
			},
			params : {
				ids : ids.join(',')
			}
		});
	}
});
var text_search_user_address = new Ext.form.TextField({
	name : 'textSearchUser',
	width : 200,
	emptyText : '请输入姓名查询!',
	listeners : {
		'specialkey' : function(field, e) {
			if (e.getKey() == Ext.EventObject.ENTER) {
				searchUserAddress();
			}
		}
	}
});

var searchUserAddress = function() {
	ds_user_address.baseParams.searchField = 'name';
	ds_user_address.baseParams.searchValue = text_search_user_address.getValue();
	ds_user_address.load({params : {start : 0,limit : 20}});
}

var btn_search_user_address = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : searchUserAddress
});

var ds_user_address = new Ext.data.Store({
	url : '/supervise/biz/useraddress/findAllUserAddress.action',
	reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, 
	[{name : 'id',type : 'int'}, 
	{name : 'orgName',type : 'string'},
	{name : 'name',type : 'string'}, 
	{name : 'email',type : 'string'}, 
	{name : 'phone',type : 'string'}, 
	{name : 'mobile',type : 'string'}, 
	{name : 'address',type : 'string'}, 
	{name : 'zipCode',type : 'string'}, 
	{name : 'sex',type : 'string'}, 
	{name : 'birthday',type : 'date', dateFormat:'Y-m-d'},
	{name : 'duty',type : 'string'},
	{name : 'standing',type : 'string'},
	{name : 'status',type : 'string'},
	{name : 'age',type : 'int'}])
});

var grid_user_address = new Ext.grid.GridPanel({
	title : '通讯录',
	iconCls : 'icon-grid',
	height : 600,
	loadMask : {msg : '数据加载中...'},
	region : 'center',
	cm : cm_user_address,
	ds : ds_user_address,
//	plugins : checkColumn,
	sm : new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
	enableColumnMove : false,
	trackMouseOver : false,
	frame : true,
	autoExpandColumn : 'address',
	tbar : [btn_add_user_address, '-', btn_edit_user_address, '-', btn_del_user_address,'-', btn_publish_user_address, 
		'-', btn_recall_user_address,'->',text_search_user_address, btn_search_user_address],
	bbar : new Ext.PagingToolbar({
		pageSize : 20,
		store : ds_user_address,
		displayInfo : true,
		displayMsg : '第 {0} - {1} 条  共 {2} 条',
		emptyMsg : "没有记录"
	}),
	// 检测至少选择一个
	checkOne : function() {
		var selections = this.getSelectionModel().getSelections();
		if (selections.length == 0) {
			Ext.MessageBox.alert("提示", "请选择一条的记录！");
			return false;
		} else if (selections.length != 1) {
			Ext.MessageBox.alert("提示", "不能选择多行！");
			return false;
		}
		return true;
	},

	// 检测必须选择一个
	checkMany : function() {
		var selections = this.getSelectionModel().getSelections();
		if (selections.length == 0) {
			Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
			return false;
		}
		return true;
	},
	listeners : {
		'render' : function(e) {
			ds_user_address.load({
				params : {
					start : 0,
					limit : 20
				}
			});
		}
	},
	stripeRows: true
});

var window_add_user_address = new Ext.Window({
	title : '添加用户',
	width : 480,
	height : 640,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('添加用户');
			Ext.getCmp("form_add_user_address").getForm().reset();
		}
	},
	items : [new Ext.FormPanel({
		id: 'form_add_user_address',
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 100,
		url : '/supervise/biz/useraddress/saveUserAddress.action',
		defaults : {anchor : '90%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{
			xtype : 'hidden',name : 'userAddress.id'
		}, {
			fieldLabel : '<span style="color: red">*&nbsp;</span>姓名',
			name : 'userAddress.name',
			allowBlank : false,
			maxLength : 30
		}, {
			xtype : 'combo',
			fieldLabel : '<span style="color: red">*&nbsp;</span>所属部门',
			hiddenName : 'userAddress.orgName',
			valueField : 'name',
			displayField : 'name',
			mode : 'remote',
			store : ds_org_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择部门'
		},{
			fieldLabel : 'email',
			id : 'userAddress.email',
			name : 'userAddress.email',
			maxLength : 30
		},{
			fieldLabel : '电话',
			name : 'userAddress.phone',
			maxLength : 30
		},{
			fieldLabel : '手机',
			id : 'userAddress.mobile',
			name : 'userAddress.mobile',
			maxLength : 30
		},{
			fieldLabel : '家庭住址',
			name : 'userAddress.address',
			maxLength : 30
		},{
			fieldLabel : '邮编',
			name : 'userAddress.zipCode',
			maxLength : 30
		}, {
			xtype : 'combo',
			fieldLabel : '性别',
			mode : 'local',
			hiddenName : 'userAddress.sex',
			editable : false,
			store : new Ext.data.SimpleStore({
				data : [['男', 'm'], ['女', 'f']],
				fields : ['text', 'value']
			}),
			displayField : 'text',
			valueField : 'text',
			mode : 'local',
			triggerAction : 'all',
			emptyText : '请选择性别'
		},/*{
			xtype: 'datefield',
			fieldLabel : '出生年月',
			name : 'userAddress.birthday',
			format : 'Y-m-d'
		},{
			xtype : 'numberfield',
			fieldLabel : '年龄',
			name : 'userAddress.age',
			allowBlank : false,
			maxLength : 70
		}, */{
			fieldLabel : '职务',
			name : 'userAddress.duty',
			maxLength : 30
		},{
			xtype : 'combo',
			fieldLabel : '人员身份',
			hiddenName : 'userAddress.standing',
			editable : false,
			store : new Ext.data.SimpleStore({
				data : data_standing,
				fields : ['text', 'value']
			}),
			displayField : 'text',
			valueField : 'value',
			mode : 'local',
			triggerAction : 'all',
			emptyText : '请选择' 
		},{
			xtype : 'hidden',
//					fieldLabel : '状态',
			name : 'userAddress.status',
			value : 'publish_no'
		}],
		buttonAlign : 'center',
		minButtonWidth : 60,
		buttons : [{
			text : '确定',
			handler : function(btn) {
				var frm = Ext.getCmp("form_add_user_address").getForm();
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('userAddress.name');
					frm.submit({
						waitTitle : '请稍候',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							var store = grid_user_address.getStore();
							if (store.data.length <= 20) {
								var user = new User({
									id : action.result.id,
									name : unfield.getValue(),
									orgName : form.findField('userAddress.orgName').getValue(),
									email : form.findField('userAddress.email').getValue(),
									phone : form.findField('userAddress.phone').getValue(),
									mobile : form.findField('userAddress.mobile').getValue(),
									address : form.findField('userAddress.address').getValue(),
									zipCode : form.findField('userAddress.zipCode').getValue(),
									sex : form.findField('userAddress.sex').getValue(),
//									birthday : form.findField('userAddress.birthday').getValue(),
									duty : form.findField('userAddress.duty').getValue()
//									age : form.findField('userAddress.age').getValue()	
								});
								store.insert(0, [user]);
								if (store.data.length > 20) {
									store.remove(store.getAt(store.data.length - 1));
								}
							}
							window_add_user_address.setTitle('[ ' + unfield.getValue() + ' ]   添加成功!!');
							unfield.reset();
							btn.enable();
							window_add_user_address.hide();
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '"' + unfield.getValue() + '" ' + '名称可能已经存在!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		}, {text : '重置',handler : function() {
				var frm =	Ext.getCmp("form_add_user_address").getForm();
				frm.reset();
				var fieldDemocracyId =	frm.findField('userAddress.democracyId');
				fieldDemocracyId.clearValue();
				fieldDemocracyId.setDisabled(true);
			}
		}, {text : '取消',handler : function() {window_add_user_address.hide();}
		}]
	})]
});
var window_edit_user_address = new Ext.Window({
	title : '修改用户',
	width : 480,
	height : 640,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('修改用户');
		}
	},
	items : [new Ext.FormPanel({
		id: 'form_edit_user_address',
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 100,
		url : '/supervise/biz/useraddress/saveUserAddress.action',
		defaults : {anchor : '90%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{
			xtype : 'hidden',name : 'userAddress.id'
		}, {
			fieldLabel : '<span style="color: red">*&nbsp;</span>姓名',
			name : 'userAddress.name',
			allowBlank : false,
			maxLength : 30
		}, {
			xtype : 'combo',
			fieldLabel : '<span style="color: red">*&nbsp;</span>所属部门',
			hiddenName : 'userAddress.orgName',
			valueField : 'name',
			displayField : 'name',
			mode : 'remote',
			store : ds_org_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择部门'
		},{
			fieldLabel : 'email',
			id : 'userAddress.email',
			name : 'userAddress.email',
			maxLength : 30
		},{
			fieldLabel : '电话',
			name : 'userAddress.phone',
			maxLength : 30
		},{
			fieldLabel : '手机',
			id : 'userAddress.mobile',
			name : 'userAddress.mobile',
			maxLength : 30
		},{
			fieldLabel : '家庭住址',
			name : 'userAddress.address',
			maxLength : 30
		},{
			fieldLabel : '邮编',
			name : 'userAddress.zipCode',
			maxLength : 30
		}, {
			xtype : 'combo',
			fieldLabel : '性别',
			mode : 'local',
			hiddenName : 'userAddress.sex',
			editable : false,
			store : new Ext.data.SimpleStore({
				data : [['男', 'm'], ['女', 'f']],
				fields : ['text', 'value']
			}),
			displayField : 'text',
			valueField : 'text',
			mode : 'local',
			triggerAction : 'all',
			emptyText : '请选择性别'
		},/*{
			xtype: 'datefield',
			fieldLabel : '出生年月',
			name : 'userAddress.birthday',
			format : 'Y-m-d'
		},{
			xtype : 'numberfield',
			fieldLabel : '年龄',
			name : 'userAddress.age',
			allowBlank : false,
			maxLength : 70
		},*/ {
			fieldLabel : '职务',
			name : 'userAddress.duty',
			maxLength : 30
		}, {
			xtype : 'combo',
			fieldLabel : '人员身份',
			hiddenName : 'userAddress.standing',
			editable : false,
			store : new Ext.data.SimpleStore({
				data : data_standing,
				fields : ['text', 'value']
			}),
			displayField : 'text',
			valueField : 'value',
			mode : 'local',
			triggerAction : 'all',
			emptyText : '请选择' 
		},{
			xtype : 'hidden',
//					fieldLabel : '状态',
			name : 'userAddress.status',
			value : 'publish_no'
		}],
		buttonAlign : 'center',
		minButtonWidth : 60,
		buttons : [{
			text : '确定',
			handler : function(btn) {
				var frm = Ext.getCmp("form_edit_user_address").getForm();
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('userAddress.name');
					frm.submit({
						waitTitle : '请稍候',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							var record = grid_user_address.getSelectionModel().getSelected();
							// set value
							record.set("orgName",frm.findField('userAddress.orgName').getValue());
							record.set("name",frm.findField('userAddress.name').getValue());
							record.set("email",frm.findField('userAddress.email').getValue());
							record.set("phone",frm.findField('userAddress.phone').getValue());
							record.set("mobile",frm.findField('userAddress.mobile').getValue());
							record.set("address",frm.findField('userAddress.address').getValue());
							record.set("zipCode",frm.findField('userAddress.zipCode').getValue());
							record.set("sex",frm.findField('userAddress.sex').getValue());
//							record.set("birthday",frm.findField('userAddress.birthday').getValue());
							record.set("duty",frm.findField('userAddress.duty').getValue());
//							record.set("age",frm.findField('userAddress.age').getValue());
							frm.reset();
							btn.enable();
							window_edit_user_address.hide();
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '"' + unfield.getValue() + '" ' + '名称可能已经存在!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		}, {text : '重置',handler : function() {
				var frm =	Ext.getCmp("form_edit_user_address").getForm();
				frm.reset();
				var fieldDemocracyId =	frm.findField('userAddress.democracyId');
				fieldDemocracyId.clearValue();
				fieldDemocracyId.setDisabled(true);
			
			}
		}, {text : '取消',handler : function() {window_edit_user_address.hide();}
		}]
	})]
});
var p_user_address = {
	id : 'address-panel',
	border : false,
	layout : 'border',
	items : [grid_user_address]
};